package com.yourcurtain.client.mvp.view.impl;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.yourcurtain.client.ClientFactory;
import com.yourcurtain.client.mvp.activity.AppActivityMapper;
import com.yourcurtain.client.mvp.place.AppPlaceHistoryMapper;
import com.yourcurtain.client.mvp.place.VideoPlace;
import com.yourcurtain.client.mvp.place.YourcurtainPlace;
import com.yourcurtain.client.mvp.view.YourcurtainView;

public class YourcurtainViewImpl extends Composite implements YourcurtainView {

	private static YourcurtainViewImplUiBinder uiBinder = GWT
			.create(YourcurtainViewImplUiBinder.class);

	private Presenter presenter;

	@UiField
	MenuItem animeMI;
	

	@UiField
	HTMLPanel mainHP;
	
	
	interface YourcurtainViewImplUiBinder extends
			UiBinder<Widget, YourcurtainViewImpl> {
	}

	public YourcurtainViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		animeMI.setCommand(new Command(){

			@Override
			public void execute() {
				 GWT.log("to anime");
					 Place videoPlace = new VideoPlace("World!");
					 SimplePanel appWidget = new SimplePanel();


				    ClientFactory clientFactory = GWT.create(ClientFactory.class);
					EventBus eventBus = clientFactory.getEventBus();
					PlaceController placeController = clientFactory.getPlaceController();
				 
					// Start ActivityManager for the main widget with our ActivityMapper
					ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
					ActivityManager activityManager = new ActivityManager(activityMapper,
							eventBus);
					activityManager.setDisplay(appWidget);
					
					
					// Start PlaceHistoryHandler with our PlaceHistoryMapper
					AppPlaceHistoryMapper historyMapper = GWT
							.create(AppPlaceHistoryMapper.class);
					PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
							historyMapper);
					historyHandler.register(placeController, eventBus, videoPlace);
					
					mainHP.add(appWidget);
					// Goes to the place represented on URL else default place
					historyHandler.handleCurrentHistory();
					

				 
				 presenter.goTo(new VideoPlace("anime"));
			}});
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

}